/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-26         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppAction;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.ColorButton;
import com.tianci.appstore.utils.SkyConstants;
import com.tianci.appstore.utils.SkyNetImageCache.ImageLoadListener;

public class CommonAppOpDialog extends CommonDialog implements ImageLoadListener {
    protected String appId;
    protected TextView contentText;
    protected LinearLayout btnLayout;

    public CommonAppOpDialog(Context context) {
        super(context);
        LogUtils.d("super after");
        initUI();
    }

    protected void initUI() {
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.setPadding(SkyConstants.Div(38), 0, SkyConstants.Div(48), SkyConstants.Div(38));

        contentText = UICommon.newTextView(mContext, SkyConstants.Dpi(38), R.color.defaultColor);
        contentText.setSingleLine(false);
        contentText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        mainLayout.addView(contentText, new LayoutParams(LayoutParams.MATCH_PARENT, SkyConstants.Div(159)));

        btnLayout = new LinearLayout(mContext);
        btnLayout.setGravity(Gravity.RIGHT);
        mainLayout.addView(btnLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    public void setData(SkyListAppData data, String content) {
        this.appId = data.getId();
        setIconTitle(data, data.getTitle());
        contentText.setText(content);

        btnLayout.removeAllViews();
    }

    public void setContent(String content) {
        contentText.setText(content);
    }

    public void addButton(AppAction action, int index, boolean hasFocus) {
        ColorButton btn = new ColorButton(mContext, action);
        addButton(btn, index, hasFocus);
    }

    public void addButton(ColorButton btn, int index, boolean hasFocus) {
        btn.setTag(index);
        btn.setOnClickListener(this);
        if (hasFocus)
            btn.requestFocus();
        if (btnLayout.getChildCount() > 0) {
            LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(
                    UICommon.btnWidth, UICommon.btnHeight);
            btnParams.leftMargin = SkyConstants.Div(30);
            btn.setLayoutParams(btnParams);
        }
        btnLayout.addView(btn);
    }

    @Override
    public void loadFinished(String url, Bitmap result) {
        // TODO Auto-generated method stub
        if (result == null)
            return;
        Message msg = mainHandler.obtainMessage();
        msg.obj = result;
        mainHandler.sendMessage(msg);
    }

    private Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            iconImage.setImageBitmap((Bitmap) msg.obj);
            super.handleMessage(msg);
        }
    };

    public void refreshContent(String contentStr) {
        contentText.setText(contentStr);
    }

    public void refreshButton(AppAction action, int index, boolean hasFocus) {
        ColorButton btn = (ColorButton) btnLayout.findViewWithTag(index);
        btn.setAction(action);
        btn.setBtnEnabled(true);
        if (hasFocus)
            btn.requestFocus();
    }

    public void refreshButton(int btnIndex, String text, int btnBg, int btnFocusBg, boolean enabled) {
        ColorButton btn = (ColorButton) btnLayout.findViewWithTag(btnIndex);
        btn.setProperty(text, btnBg, btnFocusBg, enabled);
    }

    public void setButtonVisible(int btnIndex, boolean visible) {
        ColorButton btn = (ColorButton) btnLayout.findViewWithTag(btnIndex);
        if (btn == null)
            return;
        if (visible)
            btn.setVisibility(View.VISIBLE);
        else
            btn.setVisibility(View.INVISIBLE);
        btn.setBtnEnabled(true);
    }

    public void removeAllBtns() {
        btnLayout.removeAllViews();
    }

    public int getBtnCount() {
        return btnLayout.getChildCount();
    }

    @Override
    protected void initContentSize() {
        // TODO Auto-generated method stub
        contentHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    }
}
